package com.dtsmoll.toolbar;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.constraintlayout.widget.ConstraintLayout;

public class SimpleToolbar extends ConstraintLayout {

    private ImageView returnImg;
    private ImageView refreshImg;
    private ImageView closeImg;
    private ImageView message;
    private ImageView delete;
    private TextView titleTv;
    private TextView menuTv;

    private OnClickListener onClickListener;
    // 布局View
    private View view;
    // 黑色字体
    private boolean darkFont;

    public SimpleToolbar(Context context) {
        super(context);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SimpleToolbar);
        view = LayoutInflater.from(context).inflate(R.layout.view_toolbar, this, false);
        darkFont = typedArray.getBoolean(R.styleable.SimpleToolbar_darkFont, false);
        // 初始化返回按钮
        if (typedArray.getInteger(R.styleable.SimpleToolbar_returnVisibility, GONE) == VISIBLE) {
            ViewStub returnStub = view.findViewById(R.id.toolbar_return);
            View returnView = returnStub.inflate();
            returnImg = returnView.findViewById(R.id.toolbar_img);
            returnImg.setId(R.id.toolbar_return);
            returnImg.setImageResource(darkFont ? R.drawable.ic_img_toolbar_return
                    : R.drawable.ic_img_toolbar_return_white);
        }
        // 初始化关闭按钮
        if (typedArray.getInteger(R.styleable.SimpleToolbar_closeVisibility, GONE) == VISIBLE) {
            ViewStub closeStub = view.findViewById(R.id.toolbar_close);
            View closeView = closeStub.inflate();
            closeImg = closeView.findViewById(R.id.toolbar_img);
            closeImg.setId(R.id.toolbar_close);
            closeImg.setImageResource(darkFont ? R.drawable.ic_img_toolbar_close :
                    R.drawable.ic_img_toolbar_close_white);
        }
        // 初始化刷新按钮
        if (typedArray.getInteger(R.styleable.SimpleToolbar_refreshVisibility, GONE) == VISIBLE) {
            ViewStub refreshStub = view.findViewById(R.id.toolbar_refresh);
            View refreshView = refreshStub.inflate();
            refreshImg = refreshView.findViewById(R.id.toolbar_img);
            refreshImg.setId(R.id.toolbar_refresh);
            refreshImg.setImageResource(darkFont ? R.drawable.ic_img_toolbar_refresh :
                    R.drawable.ic_img_toolbar_refresh_white);
        }
        // 初始化Title
        if (typedArray.getInteger(R.styleable.SimpleToolbar_titleVisibility, GONE) == VISIBLE
                || !TextUtils.isEmpty(typedArray.getString(R.styleable.SimpleToolbar_title))) {
            ViewStub titleStub = view.findViewById(R.id.toolbar_title);
            View titleView = titleStub.inflate();
            titleTv = titleView.findViewById(R.id.toolbar_tv);
            titleTv.setId(R.id.toolbar_title);
            titleTv.setText(typedArray.getString(R.styleable.SimpleToolbar_title));
            titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    typedArray.getDimension(R.styleable.SimpleToolbar_titleTextSize, titleTv.getTextSize()));
            titleTv.setTextColor(getResources().getColor(darkFont ? R.color.color_333333 : R.color.color_FFFFFF));
        }
        // 初始化menu TextView
        if (typedArray.getInteger(R.styleable.SimpleToolbar_menuTvVisibility, GONE) == VISIBLE
                || !TextUtils.isEmpty(typedArray.getString(R.styleable.SimpleToolbar_menuText))) {
            ViewStub menuStub = view.findViewById(R.id.toolbar_menu_tv);
            View menuView = menuStub.inflate();
            menuTv = menuView.findViewById(R.id.toolbar_menu);
            menuTv.setId(R.id.toolbar_menu_tv);
            menuTv.setText(typedArray.getString(R.styleable.SimpleToolbar_menuText));
            menuTv.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                    typedArray.getDimension(R.styleable.SimpleToolbar_menuTextSize, menuTv.getTextSize()));
            menuTv.setTextColor(getResources().getColor(darkFont ? R.color.color_333333 : R.color.color_FFFFFF));
        }
        // 初始化消息
        if (typedArray.getInteger(R.styleable.SimpleToolbar_messageVisibility, GONE) == VISIBLE) {
            ViewStub messageStub = view.findViewById(R.id.toolbar_message);
            View messageView = messageStub.inflate();
            message = messageView.findViewById(R.id.toolbar_img);
            message.setId(R.id.toolbar_message);
            message.setImageResource(darkFont ? R.drawable.ic_img_toolbar_message :
                    R.drawable.ic_img_toolbar_message_white);
        }
        // 初始化删除按钮
        if (typedArray.getInteger(R.styleable.SimpleToolbar_deleteVisibility, GONE) == VISIBLE) {
            ViewStub messageStub = view.findViewById(R.id.toolbar_delete);
            View messageView = messageStub.inflate();
            delete = messageView.findViewById(R.id.toolbar_img);
            delete.setId(R.id.toolbar_delete);
            delete.setImageResource(darkFont ? R.drawable.ic_img_toolbar_delete :
                    R.drawable.ic_img_toolbar_delete_white);
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getToolbarHeight(getContext()));
        view.setLayoutParams(layoutParams);
        view.setPadding(view.getPaddingLeft(), getStateBarHeight(getContext()), view.getPaddingRight(), view.getPaddingBottom());
        addView(view);
        typedArray.recycle();
    }

    @Override
    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        if (returnImg != null) {
            returnImg.setOnClickListener(onClickListener);
        }
        if (refreshImg != null) {
            refreshImg.setOnClickListener(onClickListener);
        }
        if (closeImg != null) {
            closeImg.setOnClickListener(onClickListener);
        }
        if (titleTv != null) {
            titleTv.setOnClickListener(onClickListener);
        }
        if (message != null) {
            message.setOnClickListener(onClickListener);
        }
        if (menuTv != null) {
            menuTv.setOnClickListener(onClickListener);
        }
        if (delete != null) {
            delete.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置标题
     */
    public void setTitleText(String title) {
        if (titleTv == null) {
            initTitleTextView();
        }
        titleTv.setText(title);
    }

    /**
     * 设置标题
     */
    public void setTitleText(@StringRes int title) {
        setTitleText(getResources().getString(title));
    }

    /**
     * 初始化title
     */
    private void initTitleTextView() {
        ViewStub titleStub = view.findViewById(R.id.toolbar_title);
        View titleView = titleStub.inflate();
        titleTv = titleView.findViewById(R.id.toolbar_tv);
        titleTv.setId(R.id.toolbar_title);
        titleTv.setTextColor(getResources().getColor(darkFont ? R.color.color_333333 : R.color.color_FFFFFF));
        if (onClickListener != null)
            titleTv.setOnClickListener(onClickListener);
    }

    /**
     * 获取状态栏高度
     */
    public static int getStateBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * 获取ShaogoodsToolbar高度
     */
    public static int getToolbarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        result = result + dptopx(50);
        return result;
    }

    /**
     * dp转px
     */
    public static int dptopx(int dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
